#include <iostream>

using std::cout;
using std::endl;

class A
{
public:
    A()
    {
        cout << "A()" << endl;
    }

    void setX(int lx)
    {
        _lx = lx;
    }

    A(long lx)
    : _lx(lx)
    {
        cout << "A(long)" << endl;
    }

    void print() const
    {
        cout << "A::_lx = " << _lx << endl;
    }

    ~A()
    {
        cout << "~A()" << endl;
    }
private:
    long _lx;
};

class B
: virtual public A
{
};

class C
: virtual public A
{
};

class D
: public B
, public C
{
public:
    D()
    {
        cout << "D()" << endl;
    }

    ~D()
    {
        cout << "~D()" << endl;
    }
};

int main(int argc, char *argv[])
{
    cout << "sizeof(A) = " << sizeof(A) << endl;
    cout << "sizeof(B) = " << sizeof(B) << endl;
    cout << "sizeof(C) = " << sizeof(C) << endl;
    cout << "sizeof(D) = " << sizeof(D) << endl;
    //菱形继承的时候：数据成员的存储二义性
    //解决：虚拟继承，让B与C虚拟继承A即可
    D d;

    d.B::setX(10);
    d.B::print();

    cout << endl;
    d.C::setX(20);
    d.C::print();

    cout << endl;
    d.A::setX(100);
    d.print();

    cout << endl;
    d.setX(200);
    d.print();

    return 0;
}

